package com.yanqu.road.server.cmd.cricket.court.cross;

import com.yanqu.road.entity.cricket.CricketMatchData;
import com.yanqu.road.entity.cricket.UserCricketData;
import com.yanqu.road.entity.cricket.court.CricketCourtBattleData;
import com.yanqu.road.entity.cricket.court.CricketCourtDefenseData;
import com.yanqu.road.entity.cricket.court.CricketCourtMatchData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.pb.CricketPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_CRICKET_MATCH_FROM_CROSS, desc = "蛐蛐跨服匹配回包")
public class CricketMatchFromCrossCmd extends AbstractCommand {

    private boolean isRefresh;

    private List<CricketProto.CricketUserBattleMatchDataTemp> userDataList;

    private int round;

    private int courtId;

    private int groupId;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CrossCricketMatchRespMsg respMsg = CricketProto.CrossCricketMatchRespMsg.parseFrom(packet.getBytes());
        ret = respMsg.getRet();
        if (respMsg.hasIsRefresh()) {
            isRefresh = respMsg.getIsRefresh();
        }
        userDataList = respMsg.getUserDataList();
        if (respMsg.hasRound()) {
            round = respMsg.getRound();
        }
        if (respMsg.hasCourtId()) {
            courtId = respMsg.getCourtId();
        }
        if (respMsg.hasGroupId()) {
            groupId = respMsg.getGroupId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CricketModule cricketModule = player.getModule(CricketModule.class);
        UserCricketData userCricketData = cricketModule.getUserCricketData();
        CricketMatchData cricketMatchData = userCricketData.getCricketMatchData();
        if (ret != 0) {
            // 返还预扣除
            if (cricketMatchData.getPreReduceProperty() != null) {
                Property preReduceProperty = cricketMatchData.getPreReduceProperty();
                cricketMatchData.setPreReduceProperty(null);
                userCricketData.setUpdateOption();
                if (!preReduceProperty.isNothing()) {
                    player.getModule(PreReduceModule.class).restorePreReduce(preReduceProperty);
                }
            }
        } else {
            if (isRefresh) {
                String cricketFightRefreshCost = GameConfig.CRICKET_FIGHT_REFRESH_COST;
                Property property = PropertyHelper.parseStringToProperty(cricketFightRefreshCost);
                if (!player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Cricket, eLogMoneyType.CricketRefreshMatch)) {
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
            }
            CricketProto.CricketMatchRespMsg.Builder respMsg = CricketProto.CricketMatchRespMsg.newBuilder();
            respMsg.setRet(0);
            respMsg.addAllUserData(userDataList);
            respMsg.setCourtId(courtId);
            respMsg.setGroupId(groupId);
            // 保存下数据
            CricketCourtMatchData courtMatchData = new CricketCourtMatchData();
            courtMatchData.setRound(round);
            courtMatchData.setCourtId(courtId);
            courtMatchData.setGroupId(groupId);
            List<CricketCourtBattleData> battleDataList = new ArrayList<>();
            for (CricketProto.CricketUserBattleMatchDataTemp battleMatchDataTemp : userDataList) {
                CricketCourtBattleData battleData = new CricketCourtBattleData();
                CricketProto.CricketUserBattleDataTemp tempUserInfo = battleMatchDataTemp.getUserInfo();
                battleData.setUserId(tempUserInfo.getUserId());
                battleData.setUserBaseInfo(PlayerBasePb.parseBaseUserInfo(tempUserInfo.getPlayerBaseData()));
                battleData.setScore(tempUserInfo.getScore());
                battleData.setCourtBadgeId(tempUserInfo.getCourtBadgeId());
                List<CricketCourtDefenseData> defenseDataList = new ArrayList<>();
                for (CricketProto.CricketMatchDataTemp dataTemp : battleMatchDataTemp.getDefenseList()) {
                    defenseDataList.add(CricketPb.parseCricketCourtDefenseData(dataTemp));
                }
                battleData.setDefenseDataList(defenseDataList);
                battleData.setWinScore(battleMatchDataTemp.getWinScore());
                battleData.setFailScore(battleMatchDataTemp.getFailScore());
                battleData.setDefenderWinScore(battleMatchDataTemp.getDefenderWinScore());
                battleData.setDefenderFailScore(battleMatchDataTemp.getDefenderFailScore());
                battleDataList.add(battleData);
            }
            courtMatchData.setBattleDataList(battleDataList);
            cricketMatchData.setTargetDefenseData(courtMatchData);
            userCricketData.setUpdateOption();
            player.sendPacket(ClientProtocol.U_CRICKET_MATCH, respMsg);
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            CricketProto.CricketMatchRespMsg.Builder respMsg = CricketProto.CricketMatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_CRICKET_MATCH, respMsg);
        }
    }
}
